//
//  MCpaixuVC.m
//  MCmeituan
//
//  Created by Mc on 15/12/16.
//  Copyright © 2015年 Mc. All rights reserved.
//

#import "MCpaixuVC.h"
#import "MCpaixuData.h"
#import "MJExtension.h"

@interface MCpaixuVC ()

@property (nonatomic, strong) NSArray *paixuData;
@property (nonatomic, strong) UIButton *paixuBtn;

@end

@implementation MCpaixuVC

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat x = 20;
    CGFloat y = 10;
    CGFloat hight = 40 ;
    CGFloat width = 140;
    NSInteger count = self.paixuData.count;
    
    for (int i = 0; i < count ; i++) {
        
        MCpaixuData *data = self.paixuData[i];
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;
        [btn setTitle:data.label forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateSelected];

        [btn imageRectForContentRect:CGRectMake(0, 0, width, hight)];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        
        btn.frame = CGRectMake(x, (hight + y ) * i  + y , width, hight);
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.preferredContentSize = CGSizeMake(width + 2 * x, (hight + y) * count + y );
}

-(NSArray *)paixuData
{
    if (_paixuData == nil) {
        
        _paixuData = [MCpaixuData objectArrayWithFilename:@"sorts.plist"];
    }
    return _paixuData;
}


- (void)btnClick:(UIButton *)btn
{   //不能这样 因为,self.paixuData[btn.tag];这是一个模型数据里面有 NSString *label 和 NSNumber* value 不仅仅是NSString
    MCpaixuData *data = self.paixuData[btn.tag];
    
    NSDictionary *dict = @{
                           @"paixu" : data
                           };
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCpaixu" object:self userInfo:dict];

    self.paixuBtn.selected = NO;
    self.paixuBtn = btn;
    self.paixuBtn.selected = YES;
}

@end
